//创建express服务
var express = require("express");
var app = express();
const fs = require("fs");
const path = require("path");
//把render转成字符串
const { renderToString } = require("@vue/server-renderer");
const render = require("@vue/server-renderer");
render.ssrRenderStyle();
const { default: createApp } = require("./dist/server.bundle");
// console.log(App);
//获取模板文件
const template = fs.readFileSync(
  path.resolve(__dirname, "./dist/index.ssr.html"),
  "utf-8"
);
// console.log(template);
app.use(express.static(path.resolve(__dirname, "./dist")));
//将模板里面的app的内容替换为server打包后的内容
const App = createApp();
// console.log(App);
app.get("/", async function (req, res) {
  //返回一个页面
  const appContent = await renderToString(App);
  console.log(appContent);
  //使用正则替换app内容
  const html = template
    .toString()
    .replace('<div id="app">', `<div id="app">${appContent}`);
  res.setHeader("Content-Type", "text/html");
  res.send(html);
});

app.listen("8087");
